* Implement the local projection method of Jorda (2005). Generates Figure A.8 and Figure A.9
*- Compute standard errors of ratio of coefficients usign xtstackreg command (requires installation)

set matsize 11000
set more off
preserve
	sort id year
	drop ratio2
	*-Rely on REER (e1) and flip its sign so that an increase is a devaluation.
	gen lRER_inverse = -lRER1
	drop lRER1 lRER
	ren lRER_inverse lRER
	gen dlRER = d.lRER
	gen dratio = d.ratio
	
	* number of lags
	local MaxLPLags 4
	
	* horizon
	local horizon 8
	local pre_horizon 4
	local tot_horizon= `pre_horizon' + `horizon'+1
	local start = `pre_horizon' + 1
	
	* LHS variable
	* index 1-4 denotes h=-4 to h=-1
	forvalues i=1/`pre_horizon' {
	local index = `start' -  `i' 
	local index2 = -1 * `i'
	gen ratio`index' = (l`i'.ratio - l.ratio)
	gen lRER`index'	   = (l`i'.lRER - l.lRER)
	label var ratio`index' "Year `index2'"
	label var lRER`index' "Year `index2'"
	}
	* index 5-13 denote h=0 to h=8
	forvalues i=0/`horizon' {	
	local index = `i' + `start'
	gen ratio`index' = (f`i'.ratio - l.ratio)
	gen lRER`index'	   = (f`i'.lRER - l.lRER)
	label var ratio`index' "Year `i'"
	label var lRER`index' "Year `i'"
	}
	label var dlRER "Change in RER"
	
	* variables to store the coefficients and SE
	gen b_ratiodlRER=.
	gen b_lRERdlRER=.
	gen se_ratiodlRER=.
	gen se_lRERdlRER=.
	
	* LP Regressions
	* One regression for each horizon of the response
	
	forvalues i=1/`tot_horizon' {
	* Imported input share LP	
	xi: xtreg ratio`i' dlRER i.year i.ccode,  fe cluster(id)
	eststo ratiodlRER`i'				
	replace b_ratiodlRER  = _b[dlRER] if _n == `i'
	replace se_ratiodlRER = _se[dlRER] if _n == `i'
	* RER LP
	xi: xtreg lRER`i' dlRER	i.year i.ccode,  fe cluster(id) 
	eststo lRERdlRER`i'	
	replace b_lRERdlRER  = _b[dlRER] if _n == `i'
	replace se_lRERdlRER = _se[dlRER] if _n == `i'	
	}
	
	* LP graphs
	eststo clear
	gen Years = _n-5 if _n <= `tot_horizon'
	local tratiodlRER "Imported Input Share"
	local tlRERdlRER "Real Exchange Rate"
	local telasticity "Elasticity"
	gen zero = 0 if _n <= `tot_horizon' // zero line
	
	* create 95% confidence interval
	scalar sig1 = 0.05	 // significance
	gen up_ratiodlRER = b_ratiodlRER + invnormal(1-sig1/2)*se_ratiodlRER if _n <= (`tot_horizon')
	gen dn_ratiodlRER = b_ratiodlRER - invnormal(1-sig1/2)*se_ratiodlRER if _n <= (`tot_horizon')
	gen up_lRERdlRER = b_lRERdlRER + invnormal(1-sig1/2)*se_lRERdlRER if _n <= (`tot_horizon')
	gen dn_lRERdlRER = b_lRERdlRER - invnormal(1-sig1/2)*se_lRERdlRER if _n <= (`tot_horizon')
	
	* main plot
	twoway (rarea up_ratiodlRER dn_ratiodlRER  Years, ///
	xlabel(-4(1)8) fcolor(gs12) lcolor(white) lpattern(solid)) ///
	(line b_ratiodlRER Years, lcolor(blue) ///
	lpattern(solid) lwidth(thick)) /// 
	(line zero Years, lcolor(black)), ///
	title("`tratiodlRER'", color(black) size(medium)) ///
	ytitle("Percent", size(medsmall)) xtitle("Year", size(medsmall)) ///
	graphregion(color(white)) plotregion(color(white)) ///
	legend(off) name(ratiodlRER_LPv2, replace)
	
	twoway (rarea up_lRERdlRER dn_lRERdlRER  Years, ///
	xlabel(-4(1)8) fcolor(gs12) lcolor(white) lpattern(solid)) ///	
	(line b_lRERdlRER Years, lcolor(blue) ///
	lpattern(solid) lwidth(thick)) /// 
	(line zero Years, lcolor(black)), ///
	title("`tlRERdlRER'", color(black) size(medium)) ///
	ytitle("Percent", size(medsmall)) xtitle("Year", size(medsmall)) ///
	graphregion(color(white)) plotregion(color(white)) ///
	legend(off) name(lRERdlRER_LPv2, replace)
	
	graph combine ratiodlRER_LPv2 lRERdlRER_LPv2, col(2) iscale(1) ///
	graphregion(color(white))
	graph export "`1'/FA8.pdf", replace
	
	* Elasticity (ratio of coefficients) with standard errors
	gen b_elas=.  
	gen se_elas=.
	foreach i of numlist 1/3 5/13 {
	xtstackreg ratio`i' lRER`i' = dlRER i.year i.id
	nlcom (elas`i':[ratio`i']dlRER/[lRER`i']dlRER), post
	replace b_elas  = _b[elas`i'] if _n == `i'
	replace se_elas  = _se[elas`i'] if _n == `i'
	}
		
	replace b_elas = 0 if _n==4
	gen up_elas = b_elas + invnormal(1-sig1/2)*se_elas if _n <= (13)
	gen dn_elas = b_elas - invnormal(1-sig1/2)*se_elas if _n <= (13)
		
	twoway (rarea up_elas dn_elas  Years if Years>=0, ///
	xlabel(0(1)8) fcolor(gs12) lcolor(white) lpattern(solid)) ///
	(line b_elas Years if Years>=0, lcolor(blue) ///
	xlabel(0(1)8) lpattern(solid) lwidth(thick)), ///
	title("`tratiodlRER'", color(black) size(medium)) ///
	ytitle("Percent", size(medsmall)) xtitle("Year", size(medsmall)) ///
	graphregion(color(white)) plotregion(color(white)) ///
	legend(off) name(elasticity, replace)
	graph export "`1'/FA9.pdf", replace
	restore
